package com.pan.common.barrier.filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * 注册filter
 * 这里可以通过filter的order实现顺序过滤
 * @author by panstark
 * @description
 * @notice
 * @date 2020/12/9
 */
@Configuration
public class filterConfig {
    /**
     * 注入一个filter，也是一个bean
     * @return
     */
    @Bean
    public Filter initFilter(){
        return new InitFilter();
    }

    @Bean
    public Filter secondFilter(){
        return new SecondFilter();
    }

    @Bean
    public FilterRegistrationBean setFilter1() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(initFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(2);   //order的数值越小，在所有的filter中优先级越高
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean setFilter2(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(secondFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(1);   //order的数值越小，在所有的filter中优先级越高
        return filterRegistrationBean;
    }
}
